package com.jiujichina.star.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jiujichina.star.base.Account;
import com.jiujichina.star.dao.EsClient;
import com.jiujichina.star.service.AccountESService;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.search.MultiMatchQuery;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHitField;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Lenovo on 2018/7/7.
 */
@Service
public class AccountESServiceImpl implements AccountESService {

    //private static final Logger LOGGER = LoggerFactory.getLogger(AccountESServiceImpl.class);

   /* @Autowired
    private JestClient jestClient;*/


   /* @Autowired
    EsClient esClient;
*/

    @Override
    public void saveEntity(Account entity) {
       /* Index index = new Index.Builder(entity).index(Account.INDEX_NAME).type(Account.TYPE).build();
        try {
            jestClient.execute(index);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }


    /**
     * 批量保存内容到ES
     */
    @Override
    public void saveEntity(List<Account> entityList) {
     /*   Bulk.Builder bulk = new Bulk.Builder();
        for(Account entity : entityList) {
            Index index = new Index.Builder(entity).index(Account.INDEX_NAME).type(Account.TYPE).build();
            bulk.addAction(index);
        }
        try {
            jestClient.execute(bulk.build());
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }

    /**
     * 在ES中搜索内容
     */
    @Override
    public List<Account> searchEntity(String searchContent){
      /*  SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchQuery("firstname",searchContent));
        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(Account.INDEX_NAME).addType(Account.TYPE).build();
        try {
            JestResult result = jestClient.execute(search);
            return result.getSourceAsObjectList(Account.class);
        } catch (IOException e) {
            //LOGGER.error(e.getMessage());
            e.printStackTrace();
        }*/
        return null;
    }

    @Override
    public List<Account> queryByPage(String queryName, int num, int size) throws Exception{
        QueryBuilder queryBuilder =  QueryBuilders.boolQuery();
        ((BoolQueryBuilder) queryBuilder).must(QueryBuilders.queryStringQuery(queryName));
        SearchResponse searchResponse = null;//esClient.queryDataPage(Account.INDEX_NAME,Account.TYPE,queryBuilder,num,size);
        List<JSONObject> jsonObjectList = new ArrayList<>();
        for (SearchHit searchHit : searchResponse.getHits().getHits()) {
           /* if (searchHit.getSourceAsMap() == null) {
                return null;
            }*/
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", searchHit.getId());
            Map<String, SearchHitField> fields = searchHit.getFields();
            for (String key : fields.keySet()) {
                jsonObject.put(key.replace("_", ""), fields.get(key).getValue());
            }
            Map<String, Object> source = searchHit.getSource();
            for (String key : source.keySet()) {
                jsonObject.put(key, source.get(key));
            }
            jsonObjectList.add(jsonObject);
        }
     /*   List<Account> accounts= jsonObjectList.stream().map(jsonObject -> {
            return  JSON.toJavaObject(jsonObject.toJSONString(),Account.class);
        }).collect(Collectors.toList());*/

        List<Account> accounts = JSON.parseArray(jsonObjectList.toString(),Account.class);
        return null;
    }
}

